package com.li.taskcenter.quartz;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: Lidong
 * @time: 2020/6/20 16:22
 **/
@Component("quartzToSpringIoc")
public class QuartzToSpringIoc extends AdaptableJobFactory {

    //AutowireCapableBeanFactory：可以将对象添加到SpringIoc的容器中，并且完成该对象的属性注入
    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    /**
     * 该方法需要将实例化的任务对象手动的添加到Springioc的容器中
     */
    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object object = super.createJobInstance(bundle);
        //将obj对象添加到SpringIoc容器中完成注入
        autowireCapableBeanFactory.autowireBean(object);
        return object;
    }
}
